package cn.js.sangu.unitStore.controller;

import cn.hutool.core.util.StrUtil;
import cn.js.sangu.unitStore.common.LogType;
import cn.js.sangu.unitStore.common.MyAuthAccess;
import cn.js.sangu.unitStore.common.MyLogsAOP;
import cn.js.sangu.unitStore.common.R;
import cn.js.sangu.unitStore.entity.Doc;
import cn.js.sangu.unitStore.entity.User;
import cn.js.sangu.unitStore.service.DocService;
import cn.js.sangu.unitStore.service.UserService;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 说明：文档接口
 * 作者：linshuo
 * 日期：2024/8/7 8:40
 */
//@CrossOrigin //已有全局跨域设置CorsConfig
@RequestMapping("doc")
@RestController
public class DocController {

    static final String OPERATION_DOCUMENT = "文档模块";

    @Autowired
    DocService docService;

    @Autowired
    UserService userService;

    @MyLogsAOP(operation = OPERATION_DOCUMENT,operaType = LogType.LOOKUP)
    @GetMapping("findByPage")
    public R findByPage(@RequestParam Integer pageNum, @RequestParam Integer pageSize, @RequestParam String title, @RequestParam String content){
        QueryWrapper<Doc> wrapper = new QueryWrapper<>();
        wrapper.like(StrUtil.isNotBlank(title),"title",title);
        wrapper.like(StrUtil.isNotBlank(content),"content",content);
        wrapper.orderByDesc("id");

        //构建page对象
        Page<Doc> param = new Page<>(pageNum, pageSize);
        Page<Doc> res = docService.page(param, wrapper);
        return R.ok(res.setRecords(this.suppleName(res.getRecords())));
    }

    @MyLogsAOP(operation = OPERATION_DOCUMENT,operaType = LogType.ADD)
    @RequestMapping("save")
    public R save(@RequestBody Doc dto){
        Doc doc = docService.saveInfo(dto);
        return R.ok("《"+doc.getTitle()+"》保存成功");
    }

    @MyLogsAOP(operation = OPERATION_DOCUMENT,operaType = LogType.UPDATE)
    @PutMapping("update")
    public R update(@RequestBody Doc dto){
        docService.updateInfo(dto);
        return R.ok("修改成功");
    }

    @MyLogsAOP(operation = OPERATION_DOCUMENT,operaType = LogType.DELETE)
    @DeleteMapping("delete/{id}")
    public R delete(@PathVariable Integer id){
        docService.removeById(id);
        return R.ok("删除成功");
    }

    @MyLogsAOP(operation = OPERATION_DOCUMENT,operaType = LogType.BATCH_DELETE)
    @DeleteMapping("deleteBatch")
    public R deleteBatch(@RequestBody List<Integer> ids){
        docService.removeBatchByIds(ids);
        return R.ok("批量删除成功");
    }

//    @MyLogsAOP(operation = OPERATION_DOCUMENT,operaType = LogType.LOOKUP)
    @MyAuthAccess
    @GetMapping("findById/{id}")
    public R findById(@PathVariable Integer id){
        Doc doc = docService.getById(id);
        User user = userService.getById(doc.getAuthor());
        if (null != user){
            doc.setAuthorName(user.getName());
        }
        return R.ok(doc);
    }

//    @MyLogsAOP(operation = OPERATION_DOCUMENT,operaType = LogType.LOOKUP)
    @GetMapping("findByRecent")
    public R findByRecent(){
        LambdaQueryWrapper<Doc> wrapper = Wrappers.lambdaQuery();
        wrapper.orderByDesc(Doc::getCreatTime).last("LIMIT 10");
        return R.ok(suppleName(docService.list(wrapper)));
    }

    /**
     * 补充文档的作者和修改人名称
     * @param list
     * @return
     */
    private List<Doc> suppleName(List<Doc> list ){
        for (Doc doc : list) {
            //作者
            User author = userService.getById(doc.getAuthor());
            if (null != author) doc.setAuthorName(author.getName());
            //修改人
            User modifier = userService.getById(doc.getModifier());
            if (null != modifier) doc.setModifierName(modifier.getName());
        }
//        list.forEach(doc -> doc.setModifierName(userService.getById(doc.getModifier()).getName()));
        return list;
    }

}
